package jihyun51.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JPasswordField;

import java.awt.Dimension;

import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.server.UID;
import java.util.Arrays;

import jihyun51.dao.MemberDAO;
import jihyun51.model.MemberVO;

public class AddMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txt_Name;
	private JTextField txt_ID;
	private JPasswordField pwd_Input;
	private JPasswordField pwd_Ok;
	private JTable table;
	MemberDAO mdao = new MemberDAO();

	// static을 이용하면 new연산자 없이 객체를 바로 사용할 수 있음
	// 하지만. static 없이 클래스 수준의 멤버변수로 선언하면
	// 모든 메서드가 다 같이 사용하게 할 수 있다.

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddMember dialog = new AddMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddMember() {
		setTitle(" :: 회원가입 :: ");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JLabel lbl_Name = new JLabel("이름");

		JLabel lbl_ID = new JLabel("아이디");

		JLabel lbl_Pwd = new JLabel("비밀번호");

		JLabel lbl_PwdOk = new JLabel("비밀번호 확인");

		JLabel lbl_Sebetsu = new JLabel("성별");

		txt_Name = new JTextField();
		txt_Name.setColumns(10);

		JRadioButton rdbtn_man = new JRadioButton("남");

		JRadioButton rdbtn_woman = new JRadioButton("여");
		rdbtn_woman.setSelected(true);

		txt_ID = new JTextField();
		txt_ID.setColumns(10);

		JButton btn_overlap = new JButton("중복확인");

		pwd_Input = new JPasswordField();
		pwd_Input.setPreferredSize(new Dimension(115, 21));

		pwd_Ok = new JPasswordField();
		pwd_Ok.setPreferredSize(new Dimension(115, 21));

		table = new JTable();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel
				.setHorizontalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				table,
																				GroupLayout.PREFERRED_SIZE,
																				1,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				lbl_Name))
														.addComponent(lbl_ID)
														.addComponent(lbl_Pwd)
														.addComponent(lbl_PwdOk)
														.addComponent(
																lbl_Sebetsu))
										.addGap(46)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																txt_Name,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				txt_ID,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btn_overlap))
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				rdbtn_man)
																		.addGap(18)
																		.addComponent(
																				rdbtn_woman))
														.addComponent(
																pwd_Input,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																pwd_Ok,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(276, Short.MAX_VALUE)));
		gl_contentPanel
				.setVerticalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lbl_Name)
														.addComponent(
																txt_Name,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																table,
																GroupLayout.PREFERRED_SIZE,
																1,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lbl_ID)
														.addComponent(
																txt_ID,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btn_overlap))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lbl_Pwd)
														.addComponent(
																pwd_Input,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lbl_PwdOk)
														.addComponent(
																pwd_Ok,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPanel
																		.createParallelGroup(
																				Alignment.BASELINE)
																		.addComponent(
																				rdbtn_man)
																		.addComponent(
																				rdbtn_woman))
														.addComponent(
																lbl_Sebetsu))
										.addContainerGap(251, Short.MAX_VALUE)));
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton(" 가입완료 ");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (!Arrays.equals(pwd_Input.getPassword(),
								pwd_Ok.getPassword())) {
							JOptionPane.showMessageDialog(null,
									"비밀번호가 일치하지 않습니다.");
						} else {
							MemberVO m = new MemberVO(null, txt_ID.getText(),
									pwd_Input.getPassword().toString(),
									txt_Name.getText(), "", null);
							if (mdao.insertMember(m) > 0)
								JOptionPane.showMessageDialog(null, "회원가입완료");
						}
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
